﻿namespace SourceCodeRepository.Web.Tests.Controllers.Snippets
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;
    using Model.Entitites;
    using Moq;
    using Persistence.Repositories.Interfaces;
    using Web.Controllers.Home.Snippets;
    using Xunit;

    public class ShowSnippetsControllerFacts
    {
        [Fact]
        public void Test_If_The_Snippet_Controller_Can_Get_All_Snippets()
        {
            var expectedSnippet = new CodeSnippet { Id = 1, SnippetText = "Test" };
            var expectedSnippet1 = new CodeSnippet { Id = 2, SnippetText = "Test2" };

            var list = new List<CodeSnippet>();
            list.Add(expectedSnippet);
            list.Add(expectedSnippet1);

            var controller = new ShowSnippetsController(this.GetMockedRepository(list));
            var result = controller.GetAllSnippets(0) as ViewResult;
            var viewData = result.ViewData.Model as IList<CodeSnippet>;

            Assert.Equal(2, viewData.Count);
            Assert.Equal(expectedSnippet, viewData.First());
            Assert.Equal(expectedSnippet1, viewData.Last());
        }

        /// <summary>
        /// Gets the mocked repository.
        /// </summary>
        /// <param name="codeSnippets">The code snippets.</param>
        /// <returns></returns>
        private ICodeSnippetRepository GetMockedRepository(IList<CodeSnippet> codeSnippets)
        {
            var mockedRepository = new Mock<ICodeSnippetRepository>();
            mockedRepository.Expect(o => o.GetAll(0)).Returns(codeSnippets);
            return mockedRepository.Object;
        }

        [Fact]
        public void Test_If_The_Controller_Can_Show_The_Default_View()
        {
            var expectedSnippet = new CodeSnippet { Id = 1, Contributor = new Contributor { Id = 1 } };
            var controller = new ShowSnippetsController(this.GetMockedRepository(new List<CodeSnippet> { expectedSnippet }));
            var view = controller.ShowView() as ViewResult;
            var viewData = view.ViewData.Model as IList<CodeSnippet>;
            Assert.Equal(1, viewData.Count);
            Assert.Equal(expectedSnippet, viewData.First());
        }
    }
}
